package demo;

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.SnapshotParameters;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.image.WritableImage;
import javafx.scene.input.ClipboardContent;
import javafx.scene.input.DataFormat;
import javafx.scene.input.DragEvent;
import javafx.scene.input.Dragboard;
import javafx.scene.input.MouseEvent;
import javafx.scene.input.TransferMode;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

// 拖拽实现文本的复制或剪切(拖拽的东西和目的节点不限)
public class DragAndCopy extends Application {
	private Pane root = new Pane();
	private Label label;
	private TextField tf;
	private StackPane sp = new StackPane();
	private ImageView iv;
	
	public static void main(String[] args) {
		launch(args);
	}
	
	@Override
	public void start(Stage stage) throws Exception {
		
		label = new Label("hello world");
		label.setLayoutX(20);
		label.setLayoutY(20);
		
		tf = new TextField();
		tf.setLayoutX(200);
		tf.setLayoutY(20);
		
		sp.setLayoutX(100);
		sp.setLayoutY(180);
		sp.setPrefSize(200, 200);
		sp.setStyle("-fx-background-color: yellow");
		
		iv = new ImageView();
		iv.setFitWidth(200);
		iv.setPreserveRatio(true);
		sp.getChildren().add(iv);
		
		root.getChildren().addAll(label, tf, sp);
		
		Scene scene = new Scene(root, 400, 400);
		stage.setScene(scene);
		stage.show();
		
		// 事件
		// 文本拖拽检测,内容放到Dragboard
		label.setOnDragDetected(new EventHandler<MouseEvent>() {
			@Override
			public void handle(MouseEvent event) {
				Dragboard db = label.startDragAndDrop(TransferMode.MOVE);	// 剪切
				
				WritableImage wi = new WritableImage((int)label.getWidth(), (int)label.getHeight());	// 改变拖拽板的样式
				label.snapshot(new SnapshotParameters(), wi);
				db.setDragView(wi);
				
				ClipboardContent content = new ClipboardContent();
				content.putString(label.getText());
				db.setContent(content);
			}
		});
		
		// 接受传输模式(同Dragboard的模式)
		tf.setOnDragOver(new EventHandler<DragEvent>() {
			@Override
			public void handle(DragEvent event) {
				event.acceptTransferModes(TransferMode.MOVE);
			}
		});
		
		// 拖拽释放
		tf.setOnDragDropped(new EventHandler<DragEvent>() {
			@Override
			public void handle(DragEvent event) {
				tf.setText(event.getDragboard().getString());
				event.setDropCompleted(true);  	// 指示拖拽完成(没有则下面无效)
			}
		});
		
		// 拖拽完成
		label.setOnDragDone(new EventHandler<DragEvent>() {
			@Override
			public void handle(DragEvent event) {
				if(event.getTransferMode() == TransferMode.MOVE) {
					label.setText("");
				}
			}
		});
		
		// 拖拽图片
		sp.setOnDragOver(new EventHandler<DragEvent>() {
			@Override
			public void handle(DragEvent event) {
				event.acceptTransferModes(event.getTransferMode());		// 获取系统的剪切模式
			}
		});
		
		sp.setOnDragDropped(new EventHandler<DragEvent>() {
			@Override
			public void handle(DragEvent event) {
				Dragboard db = event.getDragboard();
				if(db.hasFiles()) {						// 拖拽本地图片(window中没用)
					Object object = db.getContent(DataFormat.IMAGE);
					iv.setImage((Image)object);
				} else if(db.hasUrl()) {				// 拖拽网络图片
					Image imag = new Image(db.getUrl());
					iv.setImage(imag);
				}
			}
		});
	}
}
